<!DOCTYPE html>
<html>
<head>
    <% include ../include/css_header.html %>
    <style>
    </style>
</head>
<body class="actlist_bg">
<div class="container">
    <div style="margin-top: 10px;">
        <a class="icon-arrow-left" style="color: #ffffff;font-size: 24px;" href="/goodslist/<%=factionId %>"></a>
    </div>
    <div class="font_shadow_133000" style="text-align: center;color: #ffffff;">
        <h2><strong>新加商品</strong></h2>
    </div>
    <div class="row" style="margin: 0;">
        <div class="bg_op_panel" style="min-height: 620px;overflow: hidden">
            <div class="col-xs-12" style="margin-top: 10px;">
                <form id="goodsForm" method="post" enctype="multipart/form-data" action="/saveGoods">
                    <div class="form-group">
                        <label>商品名称</label>
                        <input type="text" id="name" name="name" class="form-control" placeholder="商品的名字" value="<%=goods? goods.name: ''%>">
                    </div>
                    <div class="form-group">
                        <label>商品描述</label>
                        <textarea class="form-control" rows="4" name="description" id="description" placeholder="商品描述～"><%=goods? goods.description: ''%></textarea>
                    </div>
                    <div class="form-group">
                        <label>积分</label>
                        <input type="number" id="price" name="price" class="form-control" placeholder="商品兑换积分" value="<%=goods? goods.price: ''%>">
                    </div>
                    <div class="form-group">
                        <label>数量</label>
                        <input type="number" id="count" name="count" class="form-control" placeholder="数量有限" value="<%=goods? goods.count: ''%>">
                    </div>
                    <div class="form-group">
                        <label>商品图片</label>
                        <input type="file" name="goodsImg"/>
                    </div>
                    <% if(goods){ %>
                    <div class="form-group">
                        <img src="/goods/<%=factionId %>_<%=goods._id %>.jpg" class="img-rounded" style="width: 200px;height: 200px;"/>
                    </div>
                    <input type="hidden" name="id" id="id" value="<%=goods._id %>"/>
                    <% } %>
                    <input type="hidden" name="_csrf" id="_csrf" value="<%=token %>"/>
                    <input type="hidden" name="factionId" id="factionId" value="<%=factionId %>"/>
                    <button type="button" class="btn btn-success btn-block" data-loading-text="Loading..." id="saveBtn">
                        <%=goods?'更新商品信息':'保存并上架商品'%>
                    </button>
                    <button type="button" class="btn btn-default btn-block" id="goback">返回</button>
                </form>
            </div>
        </div>
    </div>
    <% include ../include/copyright.html %>
</div>
<% include ../include/js_footer.html %>
<script>
    $(function(){
        $('#saveBtn').on('click', function(){
            saveGoods(this);
        });
        $('#goback').on('click', function(){
            window.location.href = '/goodslist'
        })
    })

    function saveGoods(obj){
        var btn = $(obj);
        if(!_checkField('name', '这是件神马商品?')) return;
        if(!_checkField('description', '来点儿描述信息')) return;
        if(!_checkField('price', '难道白送么？')) return;
        if(!_checkField('count', '有多少货?')) return;
        btn.button('loading');
        $('#goodsForm').submit();
    }
    function _checkField(id, tip){
        var val = $.trim($('#'+id).val());
        if(val === ''){
            $('#'+id).parent().addClass('has-error');
            AlertBox.showTip(tip);
            return false;
        }
        $('#'+id).parent().removeClass('has-error');
        return true
    }
</script>
</body>
</html>